package priv.project.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import priv.project.mapper.RateMapper;
import priv.project.mapper.ScenicSpotMapper;
import priv.project.mapper.UserMapper;
import priv.project.pojo.Rate;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class RateService extends ServiceImpl<RateMapper, Rate> {

    private RateMapper rateMapper;
    private UserMapper userMapper;

    private ScenicSpotMapper scenicSpotMapper;


    //新景点添加评分
    public void addRate(){

    }
//对用户的评分记录进行更新
    public boolean update(Rate entity) {
        entity.setRateSum((int) Math.ceil((entity.getRateOne()*0.2)+(entity.getRateTwo()*0.2)
                        +(entity.getRateThree()*0.15)+(entity.getRateFour()*0.25)
                        +(entity.getRateFive()*0.1)+(entity.getRateSix()*0.1)));
        QueryWrapper<Rate> rateQueryWrapper =new QueryWrapper<>();
        rateQueryWrapper.eq("user_id",entity.getUserId());
        rateQueryWrapper.eq("id",entity.getId());
        Rate ans =rateMapper.selectOne(rateQueryWrapper);
        //判断有没有评分过
        if(ans==null) {
            entity.setId(0);
            int i= rateMapper.insert(entity);
            updateRateSum(entity);
//            updateRate(entity);
            return i>0;
            //id存在说明评分过 但没有完全评分完
        }else{
            UpdateWrapper<Rate> updateWrapper =new UpdateWrapper<>();
            updateWrapper.eq("user_id",ans.getUserId());
            updateWrapper.eq("id",ans.getId());
            int i=rateMapper.update(entity,updateWrapper);
            updateRateSum(entity);
//            updateRate(entity);
            return i>0;
        }

    }

//获取该用户对该景区的评分
    public Rate getOne(Rate rate) {
        QueryWrapper<Rate> rateQueryWrapper =new QueryWrapper<>();
        rateQueryWrapper.eq("user_id",rate.getUserId());
        rateQueryWrapper.eq("scenic_id",rate.getScenicId());
        return rateMapper.selectOne(rateQueryWrapper);
    }
//未登入状态查看评分
    public Rate getUnOne(Rate rate) {
        QueryWrapper<Rate> rateQueryWrapper =new QueryWrapper<>();
        rateQueryWrapper.eq("scenic_id",rate.getScenicId());
        rateQueryWrapper.eq("user_id",0);
        return rateMapper.selectOne(rateQueryWrapper);
    }
    public void updateRateSum(Rate rate){
//        System.out.println("更新AllSum:"+rate);
        //查询userid不为0的数据
        QueryWrapper<Rate> rateQueryWrapper =new QueryWrapper<>();
        rateQueryWrapper.eq("scenic_id",rate.getScenicId());
        rateQueryWrapper.ne("user_id",0); //userid!=0
        List<Rate> rates=rateMapper.selectList(rateQueryWrapper);
        int sumUser=0;
        for (Rate value : rates) {
            sumUser += value.getRateSum();
        }
        sumUser=sumUser/rates.size();
        int scenicId=rate.getScenicId();
//        System.out.println("sumALL:"+sumUser+"scenicId:"+rate.getScenicId());
        rateMapper.updateAllRateSum(sumUser,scenicId);
    }
    //更新景点每个评分的记录
    public void updateRate(Rate rate){
        System.out.println("更新AllSum:"+rate);
        //查询userid不为0的数据
        QueryWrapper<Rate> rateQueryWrapper =new QueryWrapper<>();
        rateQueryWrapper.eq("scenic_id",rate.getScenicId());
        rateQueryWrapper.ne("user_id",0); //userid!=0
        List<Rate> rates=rateMapper.selectList(rateQueryWrapper);
        int sum1=0,sum2=0,sum3=0,sum4=0, sum5=0, sum6=0;
        for (Rate value : rates) {
            sum1 += value.getRateOne();
            sum2 += value.getRateTwo();
            sum3 += value.getRateThree();
            sum4 += value.getRateFour();
            sum5 += value.getRateFive();
            sum6 += value.getRateSix();
        }
        sum1/=rates.size();
        sum2/=rates.size();
        sum3/=rates.size();
        sum4/=rates.size();
        sum5/=rates.size();
        sum6/=rates.size();
        int scenicId=rate.getScenicId();
        rateMapper.updateRate(sum1,sum2,sum3,sum4,sum5,sum6,scenicId);
    }
    public Integer countN(Integer scenicId){
        QueryWrapper<Rate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scenic_id",scenicId);
        queryWrapper.ne("user_id",0);
        return rateMapper.selectCount(queryWrapper);
    }
}
